package org.fireflow2.designer.eclipse.parts.basic;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.NodeEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.fireflow2.designer.eclipse.figures.ActivityFigure;
import org.fireflow2.designer.eclipse.figures.widget.ActivityAnchor;
import org.fireflow2.designer.eclipse.modelwrapper.AbsWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.NodeWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.Wrapper;
import org.fireflow2.designer.eclipse.modelwrapper.basic.ActivityWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.basic.StartNodeWrapper;

public class BasicActivityPart extends AbstractGraphicalEditPart implements
		NodeEditPart, PropertyChangeListener {

	public IFigure getContentPane() {
		return ((ActivityFigure)this.getFigure()).getAttachedEventPane();
	}

	@Override
	protected List getModelChildren() {
		List children = ((ActivityWrapper) this.getModel()).getChildren();

		return children;
	}

	@Override
	protected IFigure createFigure() {
		ActivityWrapper wrapper = (ActivityWrapper) this.getModel();
		return new ActivityFigure(wrapper);
	}

	@Override
	protected void createEditPolicies() {
	}

	protected void refreshVisuals() {
		super.refreshVisuals();
		NodeWrapper wrapper = (NodeWrapper) this.getModel();
		
		
		
		//计算Activity的bounds，只增加高度，宽度暂时闲置在Activity本身的宽度之内
		Rectangle bounds = wrapper.getBox();
		List<Wrapper> children = wrapper.getChildren();
		if (children!=null && children.size()>0){
			StartNodeWrapper startNodeWrapper = (StartNodeWrapper)children.get(0);
			Rectangle childBounds = startNodeWrapper.calculateBounds();
			bounds.height= bounds.height+childBounds.height-16;
		}
		
		((GraphicalEditPart) getParent()).setLayoutConstraint(this,
				getFigure(), bounds);

		((ActivityFigure)getFigure()).refreshFigure();

		// this.getFigure()).setTasks(((Activity)diagramElm).getTasks());
	}

	public ConnectionAnchor getSourceConnectionAnchor(
			ConnectionEditPart connection) {
		// TODO Auto-generated method stub
		return new ActivityAnchor(getFigure());
	}

	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		// TODO Auto-generated method stub
		return new ActivityAnchor(getFigure());
	}

	public ConnectionAnchor getTargetConnectionAnchor(
			ConnectionEditPart connection) {
		// TODO Auto-generated method stub
		return new ActivityAnchor(getFigure());
	}

	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		// TODO Auto-generated method stub
		return new ActivityAnchor(getFigure());
	}

	@Override
	protected List getModelSourceConnections() {
		NodeWrapper modelWrapper = (NodeWrapper) this.getModel();
		return modelWrapper.getLeavingTransitions();
	}

	@Override
	protected List getModelTargetConnections() {
		NodeWrapper modelWrapper = (NodeWrapper) this.getModel();
		return modelWrapper.getEnteringTransitions();
	}

	public void propertyChange(PropertyChangeEvent evt) {
		// TODO Auto-generated method stub{

		if (evt.getPropertyName().equals(NodeWrapper.LOCATION)) {
			this.refreshVisuals();
		} else if (evt.getPropertyName()
				.equals(NodeWrapper.ENTERING_TRANSITION)){
//			ActivityWrapper wrapper = (ActivityWrapper)this.getModel();
//			if ((wrapper.getEnteringLoops().size()+wrapper.getEnteringTransitions().size())>0){
				refreshTargetConnections();
//			}
		}

		else if (evt.getPropertyName().equals(NodeWrapper.LEAVING_TRANSITION)){
//			ActivityWrapper wrapper = (ActivityWrapper)this.getModel();
//			if ((wrapper.getLeavingLoops().size()+wrapper.getLeavingTransitions().size())>0){
				refreshSourceConnections();
//			}
		}
//		else if (evt.getPropertyName().equals(NodeWrapper.TASK)||
//				evt.getPropertyName().equals(AbsWrapper.TASKREF)){
//			refreshChildren();
//		}
//		else if (evt.getPropertyName().equals(TaskWrapper.NAME)) {
//			refreshVisuals();
//		}
//		 refreshChildren();
		refresh();
	}

	public void activate() {
		if (isActive()) {
			return;
		}
		super.activate();
		((AbsWrapper) getModel()).addPropertyChangeListener(this);
	}

	public void deactivate() {
		if (!isActive()) {
			return;
		}
		super.deactivate();
		((AbsWrapper) getModel()).removePropertyChangeListener(this);
	}
}
